package com.anc.admin.demo;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class sort {
    public static void main(String[] args) {

        test4();
    }
    public static void test4() {
        String[] arr = new String[]{"12", "23", "34", "25", "26", "66", "11"};
        System.out.println("原来的字符串： " + Arrays.toString(arr));
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        System.out.println("按字符从大到小排序后的字符串： " + Arrays.toString(arr));
    }
    public static void test3() {
        String[] arr = new String[]{"AA", "CC", "KK", "MM", "GG", "FF", "DD"};
        System.out.println("原来的字符串： " + Arrays.toString(arr));
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return -o1.compareTo(o2);
            }
        });
        System.out.println("按字符从大到小排序后的字符串： " + Arrays.toString(arr));
    }

    public static void test2() {
        Fruit[] arr = new Fruit[5];
        arr[0] = new Fruit("apple", 18);
        arr[1] = new Fruit("pear", 6);
        arr[2] = new Fruit("banana", 14);
        arr[3] = new Fruit("watermelon", 26);
        arr[4] = new Fruit("tomato", 6);
        System.out.println("排序之前： " + Arrays.toString(arr));
        // 排序
        Arrays.sort(arr);
        System.out.println("排序之后： " + Arrays.toString(arr));
    }

    public static void test() {
        String[] arr = new String[]{"1", "2", "7", "4", "5", "6"};
        System.out.println("排序之前： " + Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("排序之后： " + Arrays.toString(arr));
        Arrays.sort(arr, Collections.reverseOrder());
        System.out.println("将序之后： " + Arrays.toString(arr));
    }



















   static class Fruit implements Comparable<Fruit> {
        private String name;
        private double price;

        public Fruit() {
        }

        public Fruit(String name, double price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        @Override
        public String toString() {
            return "Fruit{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    '}';
        }

        // 水果价格从低到高进行排序，若价格相同则按水果名称从高到低排序
        @Override
        public int compareTo(Fruit fruit) {
            if (this.price > fruit.price) {
                return 1;
            } else if (this.price < fruit.price) {
                return -1;
            } else {
                // this.name.compareTo(fruit.name) 按名称从低到高，前面加负号，表示相反
                return -this.name.compareTo(fruit.name);
            }
        }
    }


}
